package com.aiyige.page.home.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.aiyige.page.home.HomeDataPage;
import com.aiyige.page.home.HomeDataPageInterface;
import com.aiyige.page.home.HomeFollowPage;
import com.aiyige.page.interest.model.Interest;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2017/10/18.
 */

public class HomeViewPagerAdapter extends FragmentStatePagerAdapter {
    List<HomeDataPageInterface> pageInterfaceList = new LinkedList<>();
    HomeDataPage recommendPage;
    HomeDataPage hotPage;
    HomeFollowPage followPage;

    public HomeViewPagerAdapter(FragmentManager fm) {
        super(fm);
        recommendPage = HomeDataPage.newInstance(Interest.newBuilder()
                .category(Interest.CATEGORY_RECOMMEND)
                .build(), -3, POSITION_UNCHANGED);
        hotPage = HomeDataPage.newInstance(Interest.newBuilder()
                .category(Interest.CATEGORY_HOT)
                .build(), -2, POSITION_UNCHANGED);
        followPage = HomeFollowPage.newInstance(Interest.newBuilder()
                .category(Interest.CATEGORY_FOLLOW)
                .build(), -1, POSITION_UNCHANGED);
        pageInterfaceList.add(recommendPage);
        pageInterfaceList.add(hotPage);
        pageInterfaceList.add(followPage);
    }

    @Override
    public Fragment getItem(int position) {
        return (Fragment) pageInterfaceList.get(position);
    }

    public HomeDataPageInterface getPageInterface(int position) {
        return pageInterfaceList.get(position);
    }

    @Override
    public int getCount() {
        return pageInterfaceList.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return ((HomeDataPageInterface) object).getPosition();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return pageInterfaceList.get(position).getPageTitle();
    }

    public void setNewData(List<Interest> data) {
        //将data中没有的page移动到ViewPager最末尾
        for (HomeDataPageInterface pageInterface : pageInterfaceList) {
            if ((pageInterface.getInterest().getCategory() == Interest.CATEGORY_INDUSTRY
                    || pageInterface.getInterest().getCategory() == Interest.CATEGORY_INTEREST)
                    && !data.contains(pageInterface.getInterest())) {
                pageInterface.setSortWeight(Integer.MAX_VALUE);
            }
        }
        Collections.sort(pageInterfaceList, new Comparator<HomeDataPageInterface>() {
            @Override
            public int compare(HomeDataPageInterface o1, HomeDataPageInterface o2) {
                if (o1.getSortWeight() < o2.getSortWeight()) {
                    return -1;
                } else if (o1.getSortWeight() > o2.getSortWeight()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        for (int i = 0; i < pageInterfaceList.size(); ++i) {
            pageInterfaceList.get(i).setPosition(i);
        }
        notifyDataSetChanged();


        int dataSize = data.size();
        Iterator<HomeDataPageInterface> iterator = pageInterfaceList.iterator();
        //剔除data中没有的page
        while (iterator.hasNext()) {
            HomeDataPageInterface homeDataPageInterface = iterator.next();
            if ((homeDataPageInterface.getInterest().getCategory() == Interest.CATEGORY_INDUSTRY
                    || homeDataPageInterface.getInterest().getCategory() == Interest.CATEGORY_INTEREST)
                    && !data.contains(homeDataPageInterface.getInterest())) {
                homeDataPageInterface.setPosition(POSITION_NONE);
                iterator.remove();
            }
        }
        notifyDataSetChanged();
        int pageListSize = pageInterfaceList.size();
        //添加page中没有的data,同时配置页面排序权重
        outLoop:
        for (int i = 0; i < dataSize; ++i) {
            Interest interest = data.get(i);
            for (int j = 0; j < pageListSize; ++j) {
                if (pageInterfaceList.get(j).getInterest().equals(interest)) {
                    pageInterfaceList.get(j).setSortWeight(i);
                    continue outLoop;
                }
            }
            pageInterfaceList.add(HomeDataPage.newInstance(interest, i, 0));
        }
        notifyDataSetChanged();
        Collections.sort(pageInterfaceList, new Comparator<HomeDataPageInterface>() {
            @Override
            public int compare(HomeDataPageInterface o1, HomeDataPageInterface o2) {
                if (o1.getSortWeight() < o2.getSortWeight()) {
                    return -1;
                } else if (o1.getSortWeight() > o2.getSortWeight()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        pageListSize = pageInterfaceList.size();
        for (int i = 0; i < pageListSize; ++i) {
            pageInterfaceList.get(i).setPosition(i);
        }
        notifyDataSetChanged();
    }
}
